import { Module } from '@nestjs/common'
import { AppController } from './app.controller'
import { AppService } from './app.service'
import { TypeOrmModule } from '@nestjs/typeorm'
import { ConfigModule, ConfigService } from '@nestjs/config'
import { UsersModule } from './users/users.module'
import { AuthModule } from './auth/auth.module'
import { ItemsModule } from './items/items.module'
import { join } from 'path'
import { DefaultAdminModule, AdminUserEntity } from 'nestjs-admin'
import { AdminModule } from './admin/admin.module'
import { OrdersModule } from './orders/orders.module'

const NODE_ENV = process.env.NODE_ENV

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: !NODE_ENV
        ? ['.env.local', '.env']
        : [`.env.${NODE_ENV}.local`, `.env.${NODE_ENV}`]
    }),
    TypeOrmModule.forRootAsync({
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => ({
        type: 'postgres',
        host: configService.get('POSTGRES_HOST'),
        port: configService.get('POSTGRES_PORT'),
        username: configService.get('POSTGRES_USER'),
        password: configService.get('POSTGRES_PASSWORD'),
        database: configService.get('POSTGRES_DB'),
        entities: [join(__dirname, '**', '*.entity.{ts,js}'), AdminUserEntity],
        logging: 'all',
        synchronize: true
      })
    }),
    UsersModule,
    AuthModule,
    ItemsModule,
    OrdersModule,
    DefaultAdminModule,
    AdminModule
  ],
  controllers: [AppController],
  providers: [AppService]
})
export class AppModule {}
